<?php

require_once('Database.class.php');
require_once('placeholder.php');

$dbname = 'simpla_licenses';
$dbhost = 'localhost';
$dbuser = 'u_simpla_l';
$dbpass = 'K9PlKpcA76Ri';

$db = new Database($dbname, $dbhost, $dbuser, $dbpass);
$db->connect();

if(isset($_GET['domain']))
{
	$referer_domain = $_GET['domain'];
	$referer_domain = str_ireplace('www.', '', $referer_domain);
	$referer_domain = str_ireplace('http://', '', $referer_domain);
	if(strpos($referer_domain, '/'))
		$referer_domain = substr($referer_domain, 0 , strpos($referer_domain, '/'));
}
elseif(isset($_POST['domains'])&&!empty($_POST['domains']))
{
	$referer_domain = $_POST['domains'];
	$referer_domain = str_ireplace('www.', '', $referer_domain);
	$referer_domain = str_ireplace('http://', '', $referer_domain);
	if(strpos($referer_domain, '/'))
		$referer_domain = substr($referer_domain, 0 , strpos($referer_domain, '/'));
}
elseif(isset($_GET['host'])&&!empty($_GET['host']))
{
	$referer_domain = $_GET['host'];
	$referer_domain = str_ireplace('www.', '', $referer_domain);
	$referer_domain = str_ireplace('http://', '', $referer_domain);
	if(strpos($referer_domain, '/'))
		$referer_domain = substr($referer_domain, 0 , strpos($referer_domain, '/'));
}
elseif(isset($_SERVER['HTTP_REFERER'])&&!empty($_SERVER['HTTP_REFERER']))
{
	$referer_domain = $_SERVER['HTTP_REFERER'];
	$referer_domain = str_ireplace('www.', '', $referer_domain);
	$referer_domain = str_ireplace('http://', '', $referer_domain);
	if(strpos($referer_domain, '/'))
		$referer_domain = substr($referer_domain, 0 , strpos($referer_domain, '/'));
}else
{
	$referer_domain = '';
}

if(!empty($referer_domain))
{
	$domains = substr(trim($referer_domain), 0, 50);
	$domains = preg_replace('/[^a-zA-Z0-9\-\_\.]/', '', $domains);
}

$used = false;
$query = sql_placeholder('SELECT * FROM domains WHERE name = ?', $domains);
$db->query($query);
if ($db->results())
	$used = true;

$license = '';
if(!empty($referer_domain) && !$used)
{
  $start = date("Y-m-d", time()-60*60*24);
  $end = date("Y-m-d", time()+60*60*24*2*7);//на 2 недели

  include('license.php');
  
  $c = '';
  for($i=0; $i<rand(30, 40); $i++)
  {
  	$c .= rand(0,9);
  }
  $license = encode_license(array($domains), $end, $c);

  
  $auto = 0;
  if(isset($_GET['host'])&&!empty($_GET['host']))
  {
	$auto = 1;
  }

	
  
  $query = sql_placeholder('INSERT INTO licenses (`start`, `period`, `comment`, `user`, `user_ip`) VALUES(?, ?, ?, ?, ?)', $start, 1, 'autogenerated', 'test', $_SERVER['REMOTE_ADDR']);
  $db->query($query);
  $license_id = $db->insert_id();
  $query = sql_placeholder('INSERT INTO domains (`name`, `license_id`) VALUES(?, ?)', $domains, $license_id);
  $db->query($query);
}

if ($used) {
	if ($domains != 'localhost')
		$license = 'Для продления лицензии пишите на info@simplacms.ru';
	else
		$license = 'cdgbhchiji kjplkpppmn smsnxrtsv5 79a8998bgh hehlekghlg ojliokptnt srsaq69585 8badacbihj jfflihlijo mppqnnvptq t7799e5da6 bc8dbkhcdm mknpioqpir utup';
}

if(isset($_GET['host'])&&!empty($_GET['host']))
{
	print $license;
	exit();
}


?>
<html>
<head>
<title>Лицензия Simpla</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
td{font-family:arial; font-size:14px;text-align:left;}
th{font-family:arial; font-size:14px;text-align:left;}
input.text{font-family:arial; font-size:18px;width:500px;}
input.money{font-family:arial; font-size:18px;width:100px;}
input.submit{font-family:arial; font-size:18px;}
textarea.license{width:500px; height:200px;}
a.new{font-family:arial; font-size:18px; color:green;}
</style>
</head>
<body>


<form method=post>
<table border=0 cellpadding=10 cellspacing=10>
  <tr>
    <td>
    	Домен:
    </td>
    <td>
    	<input class=text type=text name=domains value='<?php if($domains)print $domains;else print $_POST['domains'] ?>'>
    </td>
  </tr>
  <?php if(empty($referer_domain)){ ?>
  <tr>
    <td>
    </td>
    <td>
    	<input class=submit type=submit value='Получить тестовый ключ'>
    </td>
  </tr>
  <?php } ?>
<?php
if($referer_domain)
{  
  print "<tr><td></td><td>Ваш тестовый ключ:<br><br><textarea class=license>".trim($license)."</textarea></td></tr>";
}
?>
</table>
</post>

</body>
</html>
